﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;

public partial class PropertyList : System.Web.UI.UserControl
{
    private SPList propertyList;

    private string propertyListName="Properties";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("Estate Agents")]
    public string PropertyListName
    {
        get { return propertyListName; }
        set { propertyListName = value; }
    }

    private string propertyDetailPageUrl="";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("Estate Agents")]
    public string PropertyDetailPageUrl
    {
        get { return propertyDetailPageUrl; }
        set { propertyDetailPageUrl = value; }
    }

    private string addPropertyPageUrl="";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("Estate Agents")]
    public string AddPropertyPageUrl
    {
        get { return addPropertyPageUrl; }
        set { addPropertyPageUrl = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        /* used in a test envrionment*/
        /*
        using (SPSite site = new SPSite("http://yu-pc:33333/sites/Home"))
        {
            using (SPWeb web = site.AllWebs[0])
            {
                string listName = "Properties";
                propertyList = web.Lists[listName];
                BindData(propertyList, "");    
            }
        }
         * */
   
        /* used in sharepoint context */
 
        if (string.IsNullOrEmpty(PropertyListName)) return;
     
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = SPControl.GetContextSite(Context).OpenWeb())
            {

                string listName = PropertyListName;
                propertyList = web.Lists[listName];
                BindData(propertyList, "");
            }
        }
     

        this.AddPropertyHyperLink.NavigateUrl = this.AddPropertyPageUrl;
    }


    protected void SPGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label descriptionLabel = (Label)(e.Row.Cells[0].FindControl("DescriptionContent"));
            string description = descriptionLabel.Text;
            if (description.Length > 650)
            {
                descriptionLabel.Text = description.Substring(0, 650) + "...";
            }

            Label idLabel = (Label)(e.Row.Cells[0].FindControl("IDContent"));
            HyperLink propertyDetailLink = (HyperLink)(e.Row.Cells[0].FindControl("DetailHyperLink"));
            propertyDetailLink.NavigateUrl = PropertyDetailPageUrl + "?id=" + idLabel.Text;
        }

    }

    protected void SearchButton_Click(object sender, EventArgs e)
    {
        string keywords = this.SearchTextBox.Text;
        BindData(propertyList, keywords); 

    }


    private DataTable SearchProperties(SPList list, string searchKeywords)
    {
        SPQuery qry = new SPQuery();
        searchKeywords = "";

        if (string.IsNullOrEmpty(searchKeywords.Trim()))
        {
            qry.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Address' /><FieldRef Name='Property_x0020_Status' /><FieldRef Name='Property Description' /><FieldRef Name='Price_x0020_Type' /><FieldRef Name='Property Price' /><FieldRef Name='Main_x0020_Image' />"; 
        }else
            
        {
           
             qry.Query = "<Where><Or><Or><Or><Or><Or><Or><Contains><FieldRef Name='House_x0020_Number' /><Value Type='Text'>"+searchKeywords+"</Value></Contains><Contains><FieldRef Name='Street_x0020_Line1' /><Value Type='Text'>"+searchKeywords+"</Value></Contains></Or><Contains><FieldRef Name='Street_x0020_Line2' /><Value Type='Text'>"+searchKeywords+"</Value></Contains></Or><Contains><FieldRef Name='Town' /><Value Type='Text'>"+searchKeywords+"</Value></Contains></Or><Contains><FieldRef Name='Country' /><Value Type='Text'>"+searchKeywords+"</Value></Contains></Or><Contains><FieldRef Name='Postcode' /><Value Type='Text'>"+searchKeywords+"</Value></Contains></Or><Contains><FieldRef Name='ID' /><Value Type='Counter'>"+searchKeywords+"</Value></Contains></Or></Where>";
        }


        DataTable dataTable = list.GetItems(qry).GetDataTable();

        foreach (DataRow dr in dataTable.Rows)
        {
            dr["Main_x0020_Image"] = dr["Main_x0020_Image"].ToString().Split(',')[0];
        }

        return dataTable;
    }

    private void BindData(SPList propertyList, string searchKeywords)
    {
        DataTable listItems = SearchProperties(propertyList, searchKeywords);
        this.SPGridView1.DataSource = listItems;
        this.SPGridView1.DataBind();
    }
    
}


